import { Styles } from "styles.slint";

component ScrollBar inherits Rectangle {
    in-out property <bool> horizontal;
    in-out property <length> maximum;
    in-out property <length> page-size;
    in-out property <length> value;
    in property <ScrollBarPolicy> policy: ScrollBarPolicy.as-needed;
    in property <bool> enabled;

    property <length> offset: 0px;
    property <length> size: 2px;
    property <length> track-size: root.horizontal ? root.width - 2 * root.offset : root.height - 2 * offset;
    property <length> step-size: 8px;

    visible: (self.policy == ScrollBarPolicy.always-on) || (self.policy == ScrollBarPolicy.as-needed && self.maximum > 0);
    clip: true;

    states [
        hover when touch-area.has-hover: {
            root.size: 6px;
        }
    ]

    animate size {
        duration: Styles.durations.short;
        easing: ease-out;
    }

    thumb := Rectangle {
        width: !root.horizontal ? root.size : root.maximum <= 0phx ? 0phx : max(min(Styles.sizes.h-xs, root.width), root.track-size * root.page-size / (root.maximum + root.page-size));
        height: root.horizontal ? root.size : root.maximum <= 0phx ? 0phx : max(min(Styles.sizes.h-xs, root.height), root.track-size * (root.page-size / (root.maximum + root.page-size)));
        x: !root.horizontal ? parent.width - 4px - self.width : root.offset + (root.track-size - thumb.width) * (-root.value / root.maximum);
        y: root.horizontal ? parent.height - 4px - self.height : root.offset + (root.track-size - thumb.height) * (-root.value / root.maximum);
        border-radius: (root.horizontal ? self.height : self.width) / 2;
        background: touch-area.has-hover ? Styles.palette.window-fg.with-alpha(0.6) : Styles.palette.layer-4;

        animate width, height, background {
            duration: Styles.durations.short;
            easing: ease-out;
        }
    }

    touch-area := TouchArea {
        property <length> pressed-value;

        width: parent.width;
        height: parent.height;

        pointer-event(event) => {
            if (event.button == PointerEventButton.left && event.kind == PointerEventKind.down) {
                self.pressed-value = -root.value;
            }
        }

        moved => {
            if (self.enabled && self.pressed) {
                root.value = -max(0px, min(root.maximum, self.pressed-value + (root.horizontal ? (touch-area.mouse-x - touch-area.pressed-x) * (root.maximum / (root.track-size - thumb.width)) : (touch-area.mouse-y - touch-area.pressed-y) * (root.maximum / (root.track-size - thumb.height)))));
            }
        }

        scroll-event(event) => {
            if (root.horizontal && event.delta-x != 0) {
                root.value = max(-root.maximum, min(0px, root.value + event.delta-x));
                return accept;
            } else if (!root.horizontal && event.delta-y != 0) {
                root.value = max(-root.maximum, min(0px, root.value + event.delta-y));
                return accept;
            }
            reject
        }
    }
}

export component ScrollView {
    in property <bool> enabled: true;
    out property <length> visible-width <=> flickable.width;
    out property <length> visible-height <=> flickable.height;
    in-out property <length> viewport-width <=> flickable.viewport-width;
    in-out property <length> viewport-height <=> flickable.viewport-height;
    in-out property <length> viewport-x <=> flickable.viewport-x;
    in-out property <length> viewport-y <=> flickable.viewport-y;
    in property <ScrollBarPolicy> vertical-scrollbar-policy <=> vertical-bar.policy;
    in property <ScrollBarPolicy> horizontal-scrollbar-policy <=> horizontal-bar.policy;

    in-out property <bool> has-focus;

    callback scrolled <=> flickable.flicked;

    min-height: 50px;
    min-width: 50px;
    horizontal-stretch: 1;
    vertical-stretch: 1;
    preferred-height: 100%;
    preferred-width: 100%;

    flickable := Flickable {
        interactive: false;
        viewport-y <=> vertical-bar.value;
        viewport-x <=> horizontal-bar.value;
        width: parent.width;
        height: parent.height;

        @children
    }

    vertical-bar := ScrollBar {
        enabled: root.enabled;
        width: Styles.sizes.h-xs;
        x: flickable.width + flickable.x - self.width;
        y: flickable.y + 4px;
        height: flickable.height - 8px;
        horizontal: false;
        maximum: flickable.viewport-height - flickable.height;
        page-size: flickable.height;
    }

    horizontal-bar := ScrollBar {
        enabled: root.enabled;
        width: flickable.width - 8px;
        height: Styles.sizes.h-xs;
        y: flickable.height + flickable.y - self.height;
        x: flickable.x + 4px;
        horizontal: true;
        maximum: flickable.viewport-width - flickable.width;
        page-size: flickable.width;
    }
}
